{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "67ac85bc-3e36-4362-9335-74aea32106ac",
   "metadata": {},
   "source": [
    "## 1. 定义字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "283e1731-1aa3-4dc0-b73c-4ff12f8f78fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'张三': 1.71, '李四': 1.83, '王五': 1.79, '马六': 1.68}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\"张三\": 1.71, \"李四\": 1.83, \"王五\": 1.79, \"马六\": 1.68}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1d465a4a-f514-49ff-9e79-e26918fc9d9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.71"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "身高 = {\"张三\": 1.71, \"李四\": 1.83, \"王五\": 1.79, \"马六\": 1.68}\n",
    "\n",
    "身高['张三']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73c51deb-aedb-4f0d-b214-89c3c77268fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 20, 'b': 10}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\"a\":10, \"b\": 10, \"a\":20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8c7502c-20c3-4007-9cb4-3b0da37c96b7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c084ab6e-eb97-4130-97ba-7cead9d34c62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "101"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = [100, 101, 102, 103]\n",
    "s[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f5da9272-1093-48c1-a85d-55be26325d29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 100, 1: 101, 2: 102, 3: 103}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {0:100, 1:101, 2:102, 3:103}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dcc0458f-347b-4b0b-b488-58a1857eddbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "101"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "65a6ab08-66a5-492e-a157-ee455ce4089c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "101"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = [100, 101, 102, 103]\n",
    "s[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2191617-cff2-4bd8-8cff-df77c916891c",
   "metadata": {},
   "source": [
    "<br>\n",
    "字典检索运算符无法使用切片："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0020ce78-8d2c-4b18-a799-b3b03dde4d12",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'slice'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'slice'"
     ]
    }
   ],
   "source": [
    "d[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc62c047-77c4-4cc6-a5f6-8d744e2fe120",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2cd82b53-362d-4d39-b026-a7478ec454c9",
   "metadata": {},
   "source": [
    "## 3. 字典的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e233317b-dee5-412f-8622-2b59a4b6c879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "'a' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49da799d-2f6e-4784-9547-757bf928a579",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'f' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81b5311b-1111-40eb-98da-6d75f44dfe8a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a9a02ccf-1142-49f2-9036-25125166be92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20, 'x': 80, 'y': 90}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{'a': 10, 'b': 20} | {'x': 80, 'y': 90}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b5bfa573-9800-4390-a5a3-8979de5ecc50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 25, 'c': 30}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{'a': 10, 'b': 20} | {'b': 25, 'c': 30}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "178934f2-8ba1-4f6c-946c-b550a216b26c",
   "metadata": {},
   "source": [
    "<br>\n",
    "字典新增键值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2ca4e14f-36a7-421b-b911-f675101f92a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20, 'c': 30}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "d['c'] = 30\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ef766b6-5e09-4c8b-b7e2-c0b55a4bdf4d",
   "metadata": {},
   "source": [
    "<br>\n",
    "更改键的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1c6c1e8d-896e-486a-9543-9f276f9761c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 25, 'c': 30}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['b'] = 25\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d07e7d9b-5d06-46fa-b912-284600a464bc",
   "metadata": {},
   "source": [
    "<br>\n",
    "删除字典的键值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c69ca8fe-85a7-4ee6-b9ab-00682dfd0f58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "del d['b']\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "07e9ee91-0c42-42ae-a5f6-b2fb3463b490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "d.clear()\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "56af8be8-8389-433c-8d1d-6a1411d798fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20, 'x': 80, 'y': 90}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "d.update({'x': 80, 'y': 90})\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0dd8140d-8673-4be6-92bc-e4631c2a0164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 20, 'x': 80, 'y': 90}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'a': 10, 'b': 20}\n",
    "d |= {'x': 80, 'y': 90}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a2590f6-e3ec-468d-b328-21a314408c17",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dcf1cef-0962-4866-89bd-6c8db2441ce8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
